package cn.piaoyi.service.imp;

import java.util.*;
import org.apache.commons.logging.*;
import cn.piaoyi.bean.Daily;
import cn.piaoyi.dao.DailyDAO;
import cn.piaoyi.dao.PriceDAO;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.DailyService;


public class DailyServiceImp implements DailyService {

	private static final Log logger = LogFactory.getLog(DailyServiceImp.class);
	
	private DailyDAO dailyDAO;
	private PriceDAO priceDAO;

	public DailyDAO getDailyDAO() {
		return dailyDAO;
	}

	public void setDailyDAO(DailyDAO dailyDAO) {
		this.dailyDAO = dailyDAO;
	}
	
	public PriceDAO getPriceDAO() {
		return priceDAO;
	}

	public void setPriceDAO(PriceDAO priceDAO) {
		this.priceDAO = priceDAO;
	}

	/**
	 * 保存Daily对象
	 * 
	 * @param daily
	 * 			Daily对象
	 * @return Daily
	 * 			Daily对象
	 */
	public Daily createDaily(Daily daily) throws PiaoyiException{
		try {
			daily = this.getDailyDAO().saveDaily(daily);
			return daily;
		} catch (Exception e) {
			logger.error(e);
			throw new PiaoyiException(e);
		}
	}

	/**
	 * 保存Dailys对象
	 * 
	 * @param daily
	 * 			Daily对象
	 * @return Daily
	 * 			Daily对象
	 */
	public Daily updateDaily(Daily daily) throws PiaoyiException{
		try {
			daily = this.getDailyDAO().updateDaily(daily);
			return daily;
		} catch (Exception e) {
			logger.error(e);
			throw new PiaoyiException(e);
		}
	}
	
	/**
	 * 根据主键ID获取Daily对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return Daily
	 * 			Daily对象
	 */
	public Daily findDailyById(long id){
		return this.getDailyDAO().findDailyById(id);
	}
	
	/**
	 * 获取Daily列表
	 * 
	 * @param goodsId
	 * 			long
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @return List
	 * 			Daily对象列表
	 */
	public List findDailys(long goodsId,String orderby, int ascOrDesc){
		List l = this.getDailyDAO().findDailys(goodsId, orderby, ascOrDesc);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			for(int i=0;i<l.size();i++){
				Daily d = (Daily) l.get(i);
				d.setPrices(this.getPriceDAO().findPrices(-1,d.getId(), "price", 0));
			}
			return l;
		}
	}
	
	/**
	 * 删除Daily对象
	 */
	public void removeDailyById(long id){
		this.getDailyDAO().removeDailyById(id);
	}
}
